﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataManager.BLL;

namespace FILMEK.GUI
{
    public partial class frmEladok : Form
    {
        FilmManager manager;

        string rNev;
        string rCim;
        int rFizetes;
        string rFelh;
        string rJelszo;

        public frmEladok(FilmManager manager)
        {
            InitializeComponent();
            this.manager = manager;
        }

        private void frmEladok_Load(object sender, EventArgs e)
        {
            setInputState(false);
            DataSet eladoList = manager.getAlkalmazottInfoByName("");
            fillDgvEladok(eladoList);
        }

        private void setInputState(bool state)
        {
            cbUj.Enabled = state;
            tbNev.Enabled = state;
            tbCim.Enabled = state;
            tbFizetes.Enabled = state;
            tbFelh.Enabled = state;
            tbJelszo.Enabled = state;
            btnMent.Enabled = state;
            btnMegse.Enabled = state;
        }

        private void clearInputValues()
        {
            tbNev.Text = "";
            tbCim.Text = "";
            tbFizetes.Text = "";
            tbFelh.Text = "";
            tbJelszo.Text = "";
        }

        private void fillDgvEladok(DataSet eladoList)
        {
            dgvEladok.DataSource = eladoList.Tables[0];
        }

        private void btnUj_Click(object sender, EventArgs e)
        {
            setInputState(true);
            clearInputValues();
            cbUj.Checked = true;
        }

        private void btnModosit_Click(object sender, EventArgs e)
        {
            if (dgvEladok.SelectedRows.Count != 0)
            {
                setInputState(true);
                cbUj.Checked = false;
                tbNev.Text = dgvEladok.SelectedRows[0].Cells[1].Value.ToString();
                tbCim.Text = dgvEladok.SelectedRows[0].Cells[2].Value.ToString();
                tbFizetes.Text = dgvEladok.SelectedRows[0].Cells[3].Value.ToString();
                tbFelh.Text = dgvEladok.SelectedRows[0].Cells[4].Value.ToString();
            }
        }

        private void btnTorol_Click(object sender, EventArgs e)
        {
            if (dgvEladok.SelectedRows.Count != 0)
            {
                manager.torolAlkalmazott(Convert.ToInt32(dgvEladok.SelectedRows[0].Cells[0].Value));
            }
        }

        private void dgvEladok_selectionChanged(object sender, EventArgs e)
        {
            int id;
            try
            {
                id = Convert.ToInt32(dgvEladok.SelectedRows[0].Cells[0].Value);
            }
            catch
            {
                id = 0;
                
            }
            if (id > 0)
            {
                DataSet ds = manager.getAlkalmazottAllInfoById(id);
                rNev = ds.Tables[0].Rows[0][0].ToString();
                rCim= ds.Tables[0].Rows[0][1].ToString();
                rFizetes = Convert.ToInt32(ds.Tables[0].Rows[0][2]);
                rFelh = ds.Tables[0].Rows[0][3].ToString();
                rJelszo = ds.Tables[0].Rows[0][4].ToString();
                MessageBox.Show("Megvan!  " + rNev + "  " + rCim + "  " + rFizetes + "  " + rFelh + "  " + rJelszo);
            }
        }


        private void btnMent_Click(object sender, EventArgs e)
        {
            if (cbUj.Checked)
            {
                manager.ujAlkalmazott(tbNev.Text,tbCim.Text,Convert.ToInt32(tbFizetes.Text),tbFelh.Text,tbJelszo.Text);
            }
            else
            {

                int id = Convert.ToInt32(dgvEladok.SelectedRows[0].Cells[0].Value);
                string nev = tbNev.Text;
                string cim = tbCim.Text;
                int fizetes = Convert.ToInt32(tbFizetes.Text);
                string felh = tbFelh.Text;
                string jelszo = tbJelszo.Text;

                manager.modositOptimistan(id, nev, cim, fizetes, felh, jelszo, rNev, rCim, rFizetes, rFelh, rJelszo);
                //manager.modosit(id , nev, cim, fizetes, felh, jelszo);
                DataSet d = manager.getAlkalmazottAllInfoById(id);

                if ((d.Tables[0].Rows[0][0].ToString().CompareTo(nev) == 0)
                    && (d.Tables[0].Rows[0][1].ToString().CompareTo(cim) == 0)
                    && (Convert.ToInt32(d.Tables[0].Rows[0][2]) == fizetes)
                    && (d.Tables[0].Rows[0][3].ToString().CompareTo(felh) == 0)
                    && (d.Tables[0].Rows[0][4].ToString().CompareTo(jelszo) == 0))
                {
                    MessageBox.Show("A valtoztatasok el lettek mentve!");
                }
                else
                {
                    switch (MessageBox.Show("Ido kozben modosult mar az adatbazis elem. Kivanja megis menteni a valtoztatasokat?",
                           "WonderWord",
                           MessageBoxButtons.YesNoCancel,
                           MessageBoxIcon.Question))
                    {
                        case DialogResult.Yes:
                            // "Yes" alkalmazottakat frissit
                            manager.modosit(id, nev, cim, fizetes, felh, jelszo);
                            break;

                        case DialogResult.No:
                            // "No" visszallitja a regit
                            manager.modosit(id, rNev, rCim, rFizetes, rFelh, rJelszo);
                            break;

                        case DialogResult.Cancel:
                            // "Cancel" hagyja a mostanit
                            break;
                    }
                }
            } 
            clearInputValues();
            setInputState(false);
            DataSet eladoList = manager.getAlkalmazottInfoByName("");
            fillDgvEladok(eladoList);
        }

        private void btnMegse_Click(object sender, EventArgs e)
        {
            clearInputValues();
            setInputState(false);
        }

        private void dgvEladok_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmElado myForm = null;
            myForm = new frmElado(manager, Convert.ToInt32(dgvEladok.Rows[e.RowIndex].Cells[0].Value), dgvEladok.Rows[e.RowIndex].Cells[1].Value.ToString());
            Hide();
            myForm.ShowDialog();
            Close();
        }

        private void btnSzukit_Click(object sender, EventArgs e)
        {
            DataSet eladoList = manager.getAlkalmazottInfoByName(tbSzukit.Text);
            fillDgvEladok(eladoList);
        }    

    }
}
